package com.example.woke1;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import cn.xl.xxl.imagecache.ImageLoader;

import com.http.HttpMethods;
import com.zhongcai.online.R;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.adapter.Zhouchouchildadter;
import com.woke.data.Datas_child;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class ZhongchouchildActivity extends Activity {

	private ListView mListView;
	private Zhouchouchildadter adapter;
	private Intent intent;
	private String id;
	private ArrayList<Datas_child> datalist = new ArrayList<Datas_child>();
	private ImageLoader imageLoader;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zhongchouchild);
		
		imageLoader = new ImageLoader(this);
		intent = getIntent();
		id = intent.getStringExtra("id");
		intview();
		
	}
	
	@Override
	protected void onDestroy() {
		imageLoader.clearCache();
		imageLoader.clearMemoryCache();
		super.onDestroy();
	}

	private void intview() {
		LayoutInflater inflater = getLayoutInflater();
		mListView = (ListView) findViewById(R.id.zhongchouchilid_listview);
		adapter = new Zhouchouchildadter(inflater, datalist, imageLoader, ZhongchouchildActivity.this,id);
		mListView.setAdapter(adapter);
		findViewById(R.id.zhongchouchild_image_back).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		getchilddata();
		
	}
	
	private void getchilddata() {
		AsyncHttpClient ahc = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "crowd");
		params.put("a", "crowdList");
		params.put("crowdid", id);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("自列表", ""+response);
				try {
					String flag = response.getString("flag");
					String string = response.getString("info");
					if(flag.equals("success")){
						JSONObject object = response.getJSONObject("data");
						String dtstr = object.getString("data");
						ArrayList<Datas_child> listdata = (ArrayList<Datas_child>) com.alibaba.fastjson.JSONArray.parseArray(dtstr, Datas_child.class);
							datalist.addAll(listdata);
							adapter.notifyDataSetChanged();
					}else{
						Toast.makeText(ZhongchouchildActivity.this, "获取数据失败！", Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Log.e("response", "失败"+responseString);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
//				Log.e("response", "���");
			}
		});
	}
}
